package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.client.RestTemplate;

public class WxContentSecurityUtil {

    public static String getAccessToken(String appId, String appSecret) {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        JSONObject json = JSONObject.parseObject(result);
        return json.getString("access_token");
    }

    public static boolean checkText(String accessToken, String content) {
        String url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken;
        RestTemplate restTemplate = new RestTemplate();
        JSONObject param = new JSONObject();
        param.put("content", content);
        String result = restTemplate.postForObject(url, param, String.class);
        JSONObject json = JSONObject.parseObject(result);
        // errcode为0表示内容安全
        return json.getIntValue("errcode") == 0;
    }
    public static boolean checkMedia(String accessToken, String mediaUrl, int mediaType) {
        String url = "https://api.weixin.qq.com/wxa/media_check_async?access_token=" + accessToken;
        RestTemplate restTemplate = new RestTemplate();
        JSONObject param = new JSONObject();
        param.put("media_url", mediaUrl);
        param.put("media_type", mediaType); // 2:图片, 1:音频
        String result = restTemplate.postForObject(url, param, String.class);
        JSONObject json = JSONObject.parseObject(result);
        // errcode为0表示提交成功，具体检测结果需回调
        return json.getIntValue("errcode") == 0;
    }
}